require("../util.js");
require("../component/components.js");
require("../../jsMt/util.js");
var userInfo=JSON.parse(sessionStorage.getItem("userInfo"));
if(!userInfo){
    location.href="/loginIndex"
}
var vm = new Vue({
    el:"#citySearch",
    data:{
        type:"1",  // 1、城市 2、商品  3、乐看
        // loginUid:userInfo.loginUid,
        name:"",
        isHistory:false,
        searchHistory:{},
        searchRecruitList:{}
    },
    mounted:function(){
        this.searchObject = this.getQueryStringArgs();
        this.loginUid=this.searchObject&&this.searchObject.uid;
        this.type=this.searchObject&&this.searchObject.type;
        // if(this.type==2){
        //
        // }
        this.getSearchHistory();
        //this.recruitSearchHistory();
    },
    methods:{
        //后退
        getBack:function(){
            history.go(-1);
        },
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        //搜索历史
        getSearchHistory:function(){
            var that=this,url,data;
            data={
                loginUid:userInfo.loginUid
            };
            if(that.type==2){  //商品搜索
                url="/purchase/shopSearchProductHistory";
            }else if(that.type==3){
                url="/mediaAdv/searchHistory";
            }
            this.ajax({url:url,data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.searchHistory=b.json.data;
                    //console.log(that.searchHistory)
                }else{
                    vm.$refs.prompt.toast("获取搜索历史失败！");
                }
            });
        },
        //搜索 提示框
        getSearchList:function(){
            var that=this,url,data;
            data={
                loginUid:userInfo.loginUid,
                name:that.name
            };
            if(that.type==2){  //商品搜索
                url="/purchase/searchProduct";
            }else if(that.type==3){
                url="/mediaAdv/searchMedia";
            }
            if(!data.name){
                return false;
            }
            this.ajax({url:url,data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.searchRecruitList=b.json.data;
                    //console.log(that.searchHistory)
                }else{
                    vm.$refs.prompt.toast("获取搜索历史失败！");
                }
            });
        },
        getClose:function(){
            this.name="";
        },
        //清空搜索历史
        emptySearchHistory:function(){
            var that=this,url,data;
            data={
                loginUid:userInfo.loginUid
            };
            if(that.type==2){  //商品搜索
                url="/purchase/emptySearchHistory";
            }else if(that.type==3){
                url="/mediaAdv/emptySearchHistory";
            }
            this.ajax({url:url,data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.getSearchHistory();
                }else{
                    vm.$refs.prompt.toast("删除失败！");
                }
            });
        },
        //搜索结果页
        getNext:function(title){
            location.href="/cityAd/citySearchList?title="+encodeURI(title)+"&type="+this.type+"&uid="+userInfo.loginUid;
        }
    }
    ,watch:{
        name:function(){
            var that=this;
            console.log(that.name.length);
            if(that.name.length>0){
                that.isHistory=true;
            }else{
                that.isHistory=false;
            }
            that.getSearchList();
        }
    }
});